package com.cet.lxf.collection.test09;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-08-16 17:06
 **/
public class Main01 {
    public static void main(String[] args) {
        Queue<User> q = new PriorityQueue<>(new UserComparator());
        // 添加3个元素到队列:
        q.offer(new User("Bob", "A1"));
        q.offer(new User("Alice", "A2"));
        q.offer(new User("Boss", "V1"));
        q.offer(new User("LEO","A10"));
        System.out.println(q.poll());
        System.out.println(q.poll());
        System.out.println(q.poll());
        System.out.println(q.poll());
    }
}

class UserComparator implements Comparator<User> {

    private static Pattern NUM_PATTERN = Pattern.compile("(\\w)(\\d+)");

    @Override
    public int compare(User user1, User user2) {
        if (user1.number.charAt(0) == user2.number.charAt(0)) {
            Matcher matcher1 = NUM_PATTERN.matcher(user1.number);
            Matcher matcher2 = NUM_PATTERN.matcher(user2.number);
            if (matcher1.matches() && matcher2.matches()) {
                return Integer.parseInt(matcher2.group(2)) - Integer.parseInt(matcher1.group(2));
            }
            throw new RuntimeException("用户标号不合法！");
        }
        if (user2.number.charAt(0) == 'V') {
            return 1;
        }
        return -1;
    }
}

class User {
    public final String name;
    public final String number;

    public User(String name, String number) {
        this.name = name;
        this.number = number;
    }

    @Override
    public String toString() {
        return name + "/" + number;
    }
}

